package com.tianyu.service.mqtt.model.enums;

import com.tianyu.service.mqtt.common.MqttMessageChannel;
import lombok.Getter;

import java.util.Arrays;
import java.util.regex.Pattern;


/**********************************
 * @author swyan
 * @description TopicEnum.java
 * @date 2024/3/7 - 11:17
 **********************************/

@Getter
public enum TopicEnum {

    //    - yukong/uav/state/+/+
    //    - yukong/uav/status/+/+
    //    - yukong/signal/+/+
    //    - yukong/hangar/state/+/+
    //    - yukong/message/company/+
    //    - yukong/uav/instruct_res/+/+

    UAV_STATE(Pattern.compile("^yukong/uav/state/[0-9]+/.+$"), MqttMessageChannel.ChannelName.INBOUND_UAV_STATE),

    HANGAR_UAV_STATE(Pattern.compile("^yukong/hangar/uav/state/[0-9]+/.+$"), MqttMessageChannel.ChannelName.INBOUND_HANGAR_UAV_STATE),

    UAV_STATUS(Pattern.compile("^yukong/uav/status/[0-9]+/.+$"), MqttMessageChannel.ChannelName.INBOUND_UAV_STATUS),

    SIGNAL(Pattern.compile("^yukong/signal/[0-9]+/.+$"), MqttMessageChannel.ChannelName.INBOUND_SIGNAL),

    HANGAR_STATE(Pattern.compile("^yukong/hangar/state/[0-9]+/.+$"), MqttMessageChannel.ChannelName.INBOUND_HANGAR_STATE),

    COMPANY_MESSAGE(Pattern.compile("^yukong/message/company/[0-9]+$"), MqttMessageChannel.ChannelName.INBOUND_COMPANY_MESSAGE),

    INSTRUCT_RES(Pattern.compile("^yukong/uav/instruct_res/[0-9]+/.+$"), MqttMessageChannel.ChannelName.INBOUND_INSTRUCT_RES),

    UNKNOWN(Pattern.compile("^.*$"), MqttMessageChannel.ChannelName.DEFAULT);

    final Pattern pattern;

    final String beanName;

    TopicEnum(Pattern pattern, String beanName) {
        this.pattern = pattern;
        this.beanName = beanName;
    }

    public static TopicEnum find(String topic) {
        return Arrays.stream(TopicEnum.values()).filter(topicEnum -> topicEnum.pattern.matcher(topic).matches()).findAny().orElse(UNKNOWN);
    }
}
